package com.akk.module.hlbpay.service;


import com.akk.PayConstants;
import com.akk.exception.RestBusinessException;
import com.akk.module.hlbpay.model.HlbH5PayRequest;
import com.akk.module.hlbpay.model.HlbOrderQueryRequest;
import com.akk.module.hlbpay.model.HlbOrderQueryResponse;
import com.akk.module.hlbpay.util.ConvertUtils;
import com.akk.module.hlbpay.util.Disguiser;
import com.akk.module.hlbpay.util.HlbBeanUtils;
import com.akk.module.hlbpay.util.HttpClientService;
import com.akk.utils.RestResponse;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.apache.commons.lang.StringEscapeUtils;
import org.junit.Test;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.akk.utils.RestResponse.AVS_ERROR_CODE;

@Service
public class Hlbh5Service {

    public String payRequest(HlbH5PayRequest request){
        request.setP1_bizType(PayConstants.HLB_H5_BIZ_TYPE);
        request.setP2_customerNumber(PayConstants.HLB_BUS_NUMBER);
        request.setP8_currency("CNY");
        request.setP22_channelName("UNIONPAY");
        request.setP23_isIntegral("false");// 积分
        request.setSignType("MD5");
        request.encryption();
        String oriSign = request.getSign();
        String sign = Disguiser.disguiseMD5(oriSign);
        request.setP11_payerName(StringEscapeUtils.escapeHtml(request.getP11_payerName()));
        request.setP6_goodsName(StringEscapeUtils.escapeHtml(request.getP6_goodsName()));
        request.setP24_desc(StringEscapeUtils.escapeHtml(request.getP24_desc()));
        String payContent = null;
        try {
            payContent = ConvertUtils.getAutoRequestForm2(request, PayConstants.HLB_H5_REQUEST_URL,sign);
        } catch (IllegalAccessException e) {
            throw new RestBusinessException("支付请求异常");
        }
        return payContent;
    }

    public RestResponse orderQuery(String orderNum){
        HlbOrderQueryRequest request = new HlbOrderQueryRequest();
        request.setP1_bizType(PayConstants.HLB_H5_BIZ_QUERY);
        request.setP3_customerNumber(PayConstants.HLB_BUS_NUMBER);
        request.setP2_orderId(orderNum);
        try {
            Map<String, String> map = HlbBeanUtils.convertBean(request, new LinkedHashMap());
            String oriMessage = HlbBeanUtils.getSigned(map, null,PayConstants.HLB_SIGNKEY_SAME_PERSON);
            String sign = Disguiser.disguiseMD5(oriMessage.trim());
            map.put("sign", sign);
            Map<String, Object> resultMap = HttpClientService.getHttpResp(map, PayConstants.HLB_H5_PAY_QUERY_URL);
            if ((Integer) (resultMap.get("statusCode")) == org.apache.commons.httpclient.HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                String [] excludes = {};
                HlbOrderQueryResponse response = JSONObject.parseObject(resultMsg,HlbOrderQueryResponse.class);
                String assemblyRespOriSign = HlbBeanUtils.getSigned(response, excludes,PayConstants.HLB_SIGNKEY_SAME_PERSON);
                String responseSign = response.getSign();
                String checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                if (checkSign.equals(responseSign)) {
                    return RestResponse.build(response);
                }
                return RestResponse.build(AVS_ERROR_CODE,"签名验证失败");
            }
            return RestResponse.build(AVS_ERROR_CODE,"查询失败");
        } catch (Exception e) {
            return RestResponse.build(AVS_ERROR_CODE,"查询失败");
        }
    }

    @Test
    public void test(){
        RestResponse restResponse = orderQuery(" KK41664005043710");
        System.out.println(new Gson().toJson(restResponse));
    }

}
